package com.xhwl.logistics.controller;

import com.xhwl.common.pojo.ResultJson;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.converter.mapstruct.VehicleSyncConverter;
import com.xhwl.logistics.dto.manipulate.VehicleSyncDTO;
import com.xhwl.logistics.enums.VehicleParkingLotHandlerEnum;
import com.xhwl.logistics.service.business.VehicleSyncBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

//@RestController
public class VehicleSyncController {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private VehicleSyncConverter vehicleSyncConverter;

    @Autowired
    private VehicleSyncBusinessService vehicleSyncBusinessService;

    @PostMapping("/basic-info-sync/sync-by-type")
    public ResultJson<Void> sync(@RequestBody VehicleSyncDTO vehicleSyncDTO) {

        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(vehicleSyncDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();

        IVehicleParkingLotHandler vehicleParkingLotHandler = applicationContext.getBean(VehicleParkingLotHandlerEnum.toVehicleParkingLotHandlerEnum(productId).getHandlerName(), IVehicleParkingLotHandler.class);

        vehicleParkingLotHandler.sync(vehicleIntegratedConfigBO, vehicleSyncConverter.vehicleSyncDTO2VehicleSyncBO(vehicleSyncDTO));

        return ResultJson.successWithVoid();
    }

    @PostMapping("/sync/device")
    public ResultJson<Void> syncDevice(@RequestBody VehicleSyncDTO vehicleSyncDTO) {
        VehicleSyncBO vehicleSyncBO = vehicleSyncConverter.vehicleSyncDTO2VehicleSyncBO(vehicleSyncDTO);

        vehicleSyncBusinessService.syncDevice(vehicleSyncBO);

        return ResultJson.successWithVoid();
    }

}
